package hust.cs.javacourse.search.parse.impl;

import hust.cs.javacourse.search.index.AbstractTermTuple;
import hust.cs.javacourse.search.parse.AbstractTermTupleFilter;
import hust.cs.javacourse.search.parse.AbstractTermTupleStream;
import hust.cs.javacourse.search.util.Config;
import hust.cs.javacourse.search.util.StopWords;

import java.util.ArrayList;
import java.util.Arrays;

public class LengthTermTupleFilter extends AbstractTermTupleFilter {
    /**
     * 构造函数
     * @param input：Filter的输入，类型为AbstractTermTupleStream
     */
    public LengthTermTupleFilter(AbstractTermTupleStream input) {
        super(input);
    }

    /**
     * 实现父类AbstractTermTupleStream的close方法，关闭流
     */
    @Override
    public void close(){
        super.close();
    }

    /**
     * 过滤掉过短或过长的单词
     * @return
     */
    @Override
    public AbstractTermTuple next(){
        AbstractTermTuple tempTuple = input.next();
        if(tempTuple==null) return null;
        //过滤掉长度小于3或者长度大于20的单词
        while(tempTuple.term.getContent().length() < Config.TERM_FILTER_MINLENGTH || tempTuple.term.getContent().length() > Config.TERM_FILTER_MAXLENGTH){
            tempTuple = input.next();
            if(tempTuple==null) return null;
        }
        return tempTuple;//返回合格的、没有被过滤的termTuple
    }
}
